#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>		 
#include <fcntl.h>
#include <pthread.h>
#include <termios.h>
#include <errno.h>
#include "serial-ops.h"
#include "public.h"

void extract_bd(char *data, int data_length)
{
	int i;
	int flag = 0;
	char hex[RTCM_LEN] = {0};
	
	pk.flag_bd = 0;

	for (i = 0; i < data_length; i++) {
		if ((data[i] == '$') && (data[i+1] == 'G')) {
			flag = 1;	/* GGA flag */
		}
		
		if ((flag == 1) && (data[i] == 0xd) && (data[i+1] == 0xa)) {
			flag = 2;	/* GGA end */
			break;
		}
	}

	if ((flag == 2) && (data_length > (i + 8))) {
		memset(pk.gga_bd, 0, GGA_LEN);
		memcpy(pk.gga_bd, data, i);
		pk.flag_bd |= GGA_VALID;
		//printf("pk_bd gga:%d:%s\n", pk.flag_bd, pk.gga_bd);

		memset(pk.rtcm_bd.transfer, 0, RTCM_LEN);
		hex_to_char(hex, (unsigned char *)data+i+2, data_length-i-2);
		//printf("ac hex rtcm %d:%s\n", strlen(hex), hex);
		strncpy(pk.rtcm_bd.transfer, hex, strlen(hex));
		pk.flag_bd |= RTCM_VALID;
		//printf("pk_bd rtcm %d:%d:%s\n", pk.flag_bd, strlen(pk.rtcm_bd.transfer), pk.rtcm_bd.transfer);
	}
}

